package com.vois.jack.btmgr.classicbase;

import com.vois.jack.btmgr.common.DeviceRecorderControlInterface;
import com.vois.jack.btmgr.devices.WL100Dev.G711;
import com.vois.jack.btmgr.util.DataRingBuffer;
import com.vois.jack.btmgr.util.Logger;

public class BtSppRecorder implements BtRecorderInterface{
    private Logger logger = Logger.getLogger(BtSppRecorder.class);
    private DataRingBuffer dataRingBuffer;
    private boolean isRecorderCreated;
    private boolean isRecorderStarted;
    private final int MAX_AUDIO_BUFFER_SIZE = 48000 * 2;

    @Override
    public void create(int audioSource, int sampleRate) {
        dataRingBuffer = new DataRingBuffer("BtSppRecorder", MAX_AUDIO_BUFFER_SIZE, true);
        isRecorderCreated = true;
    }

    @Override
    public void release() {
        if (isRecorderCreated) {
            dataRingBuffer.destroyBuffer();
            isRecorderCreated = false;
        }
    }

    @Override
    public int getAudioSource() {
        return 0;
    }

    @Override
    public int getSampleRate() {
        return 8000;
    }

    @Override
    public void startRecord() {
        if (!isRecorderStarted) {
            isRecorderStarted = true;
            try {
                dataRingBuffer.clearBuffer();
            }catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void stopRecord() {
        if (isRecorderStarted) {
            isRecorderStarted = false;
        }
    }

    @Override
    public int putRecordData(byte[] rawData, int offset, int rawDataSize) {
        if (isRecorderCreated && dataRingBuffer != null) {
            try {
                int putSize = dataRingBuffer.putData(rawData, offset, rawDataSize);
                if (putSize > 0) {
                    return putSize;
                }
            } catch (InterruptedException e) {
                if (e.getMessage() != null) {
                    logger.e("data ring buffer exception !!" + e.getMessage());
                }else {
                    logger.e("data ring buffer exception !!");
                }
            }
        }

        return -1;
    }

    @Override
    public int readRecordData(short[] audioData, int offsetInShort, int sizeInShort) {
        byte[] data = new byte[sizeInShort];
        int getSize = -1;
        int offsetNow = 0;
        int leftSize = sizeInShort;

        //logger.d("readRecordData: size: " + dataRingBuffer.getDataSize());
        try {
            while (dataRingBuffer != null && leftSize > 0) {
                getSize = dataRingBuffer.getData(data, 0, leftSize);
                for (int i = 0; i < getSize; i++) {
                    audioData[offsetInShort + i + offsetNow] = (short) G711.alaw2linear(data[i]);
                }
                offsetNow += getSize;
                leftSize -= getSize;
            }
        }catch (InterruptedException e) {
            e.printStackTrace();
        }

        return sizeInShort;
    }

    @Override
    public int getAudioSessionId() {
        return -1;
    }

    @Override
    public int putEncodedRecordData(byte[] rawData, int offsetInByte, int rawDataSize) {
        return 0;
    }

    @Override
    public int getEncodedRecordData(byte[] rawData) {
        return 0;
    }

    @Override
    public int getRecordDataType() {
        return RAW_RECORD_DATA;
    }
}
